package me.zhengjie.config.webConfig;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

/**
 * @author Yaco
 * @title: RequestWrapper
 */
public class RequestWrapper extends HttpServletRequestWrapper {
    private final String body;
    private Boolean printFlag;

    public RequestWrapper(HttpServletRequest request,Boolean printFlag) throws IOException {
        super(request);
        if(printFlag != null){
            this.printFlag = printFlag;
        }else{
            this.printFlag = false;
        }
        if(printFlag){
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = null;
            try {
                InputStream inputStream = request.getInputStream();
                if (inputStream != null) {
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    char[] charBuffer = new char[128];
                    int bytesRead = -1;
                    while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                        stringBuilder.append(charBuffer, 0, bytesRead);
                    }
                } else {
                    stringBuilder.append("");
                }
            } catch (IOException ex) {
                throw ex;
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException ex) {
                        throw ex;
                    }
                }
            }
            body = stringBuilder.toString();
        }else{
            body = "";
        }
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if(printFlag){
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes("UTF-8"));
            ServletInputStream servletInputStream = new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return false;
                }

                @Override
                public boolean isReady() {
                    return false;
                }

                @Override
                public void setReadListener(ReadListener readListener) {
                }

                @Override
                public int read() throws IOException {
                    return byteArrayInputStream.read();
                }
            };
            return servletInputStream;
        }else{
            return super.getInputStream();
        }
    }

    @Override
    public BufferedReader getReader() throws IOException {
        if(printFlag){
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }else{
            return super.getReader();
        }
    }

    public String getBody() {
        return this.body;
    }



}

